﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace jnlamp.Web.userinfo
{
    /// <summary>
    /// GetLinker 的摘要说明
    /// </summary>
    public class GetLinker : IHttpHandler, IRequiresSessionState
    {
        jnlamp.BLL.mylinker bll = new jnlamp.BLL.mylinker();
        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        StringBuilder rechild = new StringBuilder();
        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);            
            else if (ac == "add")
                add(context);            
            else if (ac == "delete")
                delete(context);
            else if (ac == "queryt")//获得树
                Queryinfo(context);            

        }
       
        public void Queryinfo(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                uid = Web.Admin.myinfo.getID(context);                
                string sql = " and uid=" + uid;
                DataSet ds= bll.GetList(sql);
                string str = GetTreeJsonByTableOne(ds.Tables[0], "luid", "uname", "utruename");
                
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }

        }
   
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["mlid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                if (bll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
       
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = Web.Admin.myinfo.getID(context);
                int luid = 0;
                string st = "";
                if (null != context.Request.Form["uname"])
                {//获取前台传来的值  
                    st = context.Request.Form["uname"].ToString().Trim();
                    if (st != "")
                    {
                        BLL.userinfo u = new BLL.userinfo();
                        luid = u.GetIDByName(st);
                    }
                }
                if (luid != 0)
                {
                    jnlamp.Model.mylinker model = new jnlamp.Model.mylinker();
                    model.uid = uid;
                    model.luid = luid;
                    model.addtime = DateTime.Now;                    
                    int rid = bll.Add(model);
                    if (rid > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("用户已添加！");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("没有找到用户！");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
  
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {                
                string uname, utruename, st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                int uid = 0;
                uid = Web.Admin.myinfo.getID(context); 
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" uname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", utruename);
                }
                strWhere.AppendFormat(" uid='{0}' ", uid);
                //int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                //if (startindex >= 0)
                //{
                //    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                //}
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }

        private string GetTreeJsonByTableOne(DataTable tabel, string idCol, string txtCol, string bmCol)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[{\"id\":\"0\",\"text\":\"所有\",\"iconCls\":\"icon-user\",\"children\":[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"iconCls\":\"icon-user\",\"text\":\"" + row[txtCol] + "-" + row[bmCol] + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]}]");

            }
            return result.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}